package com.contest.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.contest.entity.BettingRecord;
import com.contest.util.FileUtil;
import com.contest.util.IdWorker;
import com.contest.util.PageUtils;
import com.contest.util.R;
import com.contest.ws.WebSocketSessionManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.*;

import com.contest.entity.RaceInfo;
import com.contest.service.RaceInfoService;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.socket.WebSocketSession;

import javax.servlet.http.HttpServletRequest;


/**
 * @author yfl
 * @email 617823298@qq.com
 * @date 2017-12-20 16:30:37
 */

@Controller
@RequestMapping("/raceInfo")
public class RaceInfoController {

    @Autowired
    RaceInfoService raceInfoService;
    @Autowired
    IdWorker idWorker;

    @RequestMapping("/list")
    @ResponseBody
    public PageUtils list(@RequestParam Map<String,Object> param) {
        int page = Integer.parseInt(param.get("page").toString());
        int rows = Integer.parseInt(param.get("rows").toString());
        Page<RaceInfo> mPage = new Page<RaceInfo>((page - 1) * rows, rows);

        Wrapper<RaceInfo> w = new EntityWrapper<>();
        String o = String.valueOf(param.get("name"));
        if(o != null && !o.equals("") && !o.equals("null")) {
            w.like("fd_name",o);
        }

        mPage = raceInfoService.selectPage(mPage,w);
        return new PageUtils(mPage.getRecords(),mPage.getTotal());
    }

    @RequestMapping("/comboList")
    @ResponseBody
    public List<RaceInfo> comboList() {
        List<RaceInfo> raceInfoList = raceInfoService.selectList(null);
        return raceInfoList;
    }

    @PostMapping("/save")
    @ResponseBody
    public R save(HttpServletRequest request, @RequestParam("racePic") CommonsMultipartFile racePic, RaceInfo raceInfo) {
        String saveName = null;
        if (racePic != null && !racePic.getOriginalFilename().equals("")) {
            saveName = FileUtil.uploadFile(request, racePic);
            raceInfo.setFdRacePic(saveName);
        }
        raceInfo.setFdId(idWorker.nextId() + "");
        if (raceInfoService.insert(raceInfo)) {
            return R.ok("操作成功!");
        } else {
            return R.error("操作失败");
        }
    }

    @GetMapping("/edit/{fdId}")
    public String edit(Model model, @PathVariable("fdId") String fdId) {
        RaceInfo raceInfo = raceInfoService.selectById(fdId);
        model.addAttribute("raceInfo", raceInfo);
        return "raceInfo/edit";
    }

    @PostMapping("/remove")
    @ResponseBody
    public R remove(@RequestParam("ids[]") String[] ids) {
        List<String> fdIds = new ArrayList<String>();
        for (String id : ids) {
            fdIds.add(id);
        }
        if (raceInfoService.deleteBatchIds(fdIds)) {
            return R.ok("删除成功");
        } else {
            return R.error(-1, "删除失败!");
        }
    }

    @PostMapping("/update")
    @ResponseBody
    public R update(HttpServletRequest request, @RequestParam("racePic") CommonsMultipartFile racePic,RaceInfo raceInfo) {
        String saveName = null;
        if (racePic != null && !racePic.getOriginalFilename().equals("")) {
            saveName = FileUtil.uploadFile(request, racePic);
            raceInfo.setFdRacePic(saveName);
        }
        if (raceInfoService.updateById(raceInfo)) {
            return R.ok("更新成功!");
        } else {
            return R.error(-1, "更新失败!");
        }
    }


    //赛事列表
    @RequestMapping("/client/raceList")
    @ResponseBody
    public R gameList(String gameId) {
        Wrapper<RaceInfo> raceInfoWrapper = new EntityWrapper<RaceInfo>();
        raceInfoWrapper.addFilter("fd_game_id={0}", gameId);
        List<RaceInfo> raceInfoList = raceInfoService.selectList(raceInfoWrapper);
        return R.ok(raceInfoList);
    }

}
